package com.tbvtc.agriculturalcreditchainserver2025.utils.recognize;

import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class FieldDataConverter {

    /**
     * 转换原始数据为英文key的结构
     */
    public Map<String, Object> convert(Map<Integer, String> originalData) {
        // 1. 按top值排序
        List<Map.Entry<Integer, String>> sortedEntries = new ArrayList<>(originalData.entrySet());
        sortedEntries.sort(Comparator.comparingInt(Map.Entry::getKey));

        // 2. 定义英文key容器
        Map<String, Object> root = new LinkedHashMap<>();
        Map<String, Object> fieldRecord = new LinkedHashMap<>(); // 田间档案（fieldRecord）
        Map<String, String> grapeInfo = new HashMap<>(); // 葡萄档案（grapeInfo）
        List<Map<String, String>> fertilizerRecords = new ArrayList<>(); // 施肥记录
        List<Map<String, String>> pesticideRecords = new ArrayList<>(); // 农药记录

        // 临时记录容器
        Map<String, String> currentFertilizer = null;
        Map<String, String> currentPesticide = null;

        // 3. 遍历处理数据
        for (Map.Entry<Integer, String> entry : sortedEntries) {
            String value = entry.getValue();

            // 跳过"田间档案"标题
            if ("田间档案".equals(value)) {
                continue;
            }

            // 处理施肥记录
            if ("施肥记录1".equals(value) || "施肥记录2".equals(value)) {
                currentFertilizer = new HashMap<>();
                fertilizerRecords.add(currentFertilizer);
                currentPesticide = null;
                continue;
            }

            // 处理农药记录
            if ("农药记录1".equals(value) || "农药记录2".equals(value)) {
                currentPesticide = new HashMap<>();
                pesticideRecords.add(currentPesticide);
                currentFertilizer = null;
                continue;
            }

            // 拆分键值对并转换为英文key
            String[] keyValue = splitAndTranslate(value);
            if (keyValue == null) {
                continue;
            }
            String enKey = keyValue[0]; // 英文key
            String val = keyValue[1];   // 值

            // 存入对应容器
            if (currentFertilizer != null) {
                currentFertilizer.put(enKey, val);
            } else if (currentPesticide != null) {
                currentPesticide.put(enKey, val);
            } else {
                grapeInfo.put(enKey, val);
            }
        }

        // 4. 组装英文key结构
        root.put("grapeInfo", grapeInfo);
        root.put("fertilizerRecords", fertilizerRecords);
        root.put("pesticideRecords", pesticideRecords);
//        root.put("fieldRecord", fieldRecord);

        return root;
    }

    /**
     * 拆分键值对并将中文key转换为英文（已移除温度、湿度、光照和土壤湿度）
     */
    private String[] splitAndTranslate(String value) {
        int colonIndex = value.indexOf(": ");
        if (colonIndex == -1) {
            return null;
        }
        String cnKey = value.substring(0, colonIndex);
        String val = value.substring(colonIndex + 2);

        // 中文key转英文映射（已移除温度、湿度、光照和土壤湿度相关映射）
        String enKey;
        switch (cnKey) {
            case "档案名称":
                enKey = "fieldRecordName";
                break;
            case "土地名称":
                enKey = "landName";
                break;
            case "农作物名称":
                enKey = "cropName";
                break;
            case "阶段信息":
                enKey = "stageInfo";
                break;
            case "肥料名称":
                enKey = "fertilizerName";
                break;
            case "施肥时间":
                enKey = "fertilizerTime";
                break;
            case "肥料用量":
                enKey = "fertilizerAmount";
                break;
            case "购买渠道":
                enKey = "purchaseChannel";
                break;
            case "农药名称":
                enKey = "pesticideName";
                break;
            case "施药时间":
                enKey = "pesticideTime";
                break;
            case "农药用量":
                enKey = "pesticideAmount";
                break;
            default:
                enKey = cnKey; // 默认保留原key（防止遗漏）
                break;
        }
        return new String[]{enKey, val};
    }
}
